
let user = document.querySelector("#user")
let pwd = document.querySelector("#pwd")
let sign = document.querySelector("#sign")
let login = document.querySelector("#login")


console.log(user)
console.log(pwd)
console.log(sign)
console.log(login)

sign.addEventListener("click", () => {
    window.location.href = "./html/sign.html"
})

login.addEventListener("click", (e) => {
    e.preventDefault()
    let user_id = user.value
    let user_pwd = pwd.value
    let states = [0, 0]
    if (user_id == "") {
        console.log("请填写账号")
    } else {
        states[0] = 1
    }
    if (user_pwd == "") {
        console.log("请填写密码")
    } else {
        states[1] = 1
    }

    if (states[0] == 1 && states[1] == 1) {
        new Promise((resolve,rejects)=>{
            $.ajax({
                url: `/Mylogin?user_id=${user_id}&user_pwd=${user_pwd}`,
                success(msg) {
                    // console.log(msg)
                    resolve(msg)
                }
            })
        }).then((msg)=>{
            if(msg==1){
                console.log("登录成功")
                document.cookie=`user={"user":"${user_id}","pwd":"${user_pwd}"}`
                window.location.href="./html/chat.html"
            }else {
                alert("账号密码错误")
                user.value=""
                pwd.value=""
            }
        })
    }

})

// function writeCookie(cookieName,cookieValue,expires){
//     let cookiestr=`cookieName={user_id:${cookieValue.user},user_pwd:${cookieValue.pwd}};expires=${}`;
//     document.cookie=
// }